const http = require("http");
const fs = require("fs/promises");
const url = require("url");
const path = require("path");
const os = require("os");
const chalk = require("chalk");
class Server {
  constructor(options = {}) {
    this.port = options.port;
    this.dir = options.dir;
    this.hosts = Object.values(os.networkInterfaces())
      .flat()
      .filter((item) => item.family === "IPv4")
      .map((item) => `http://${item.address}:${chalk.green(this.port)}`);
  }
  requestCallback = async (req, res) => {
    try {
      const { pathname, query } = url.parse(req.url);
      const accessPath = path.join(this.dir, pathname);
      const statObj = await fs.stat(accessPath);
      if (statObj.isDirectory()) {
        let dirs = await fs.readdir(accessPath);
        console.log(dirs);
      }
    } catch (e) {
      console.log(e);
    }
  };
  start() {
    const server = http.createServer(this.requestCallback);
    server.listen(this.port, () => {
      this.hosts.forEach((host) => {
        console.log(host);
      });
    });
  }
}
const server = new Server({
  port: 3000,
  dir: process.cwd(),
});
server.start();

// 处理我们的命令行，
// 包发到npm上
// 处理文件夹的访问
// 实现缓存 ， 跨域的处理
// 文件的压缩。。。。

// express koa
